package com.atgm.mall.gmmailproduct.service.impl;

import com.atgm.common.exception.GameException;
import com.atgm.common.utils.GmMallUtils;
import com.atgm.mall.gmmailproduct.dao.GameAttrRelaDao;
import com.atgm.mall.gmmailproduct.entity.GameAttrRelaEntity;
import com.atgm.mall.gmmailproduct.service.GameAttrRelaService;
import com.atgm.mall.gmmailproduct.vo.req.GameAttrReqVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
@Service
public class GameAttrRelaServiceImpl extends ServiceImpl<GameAttrRelaDao, GameAttrRelaEntity> implements GameAttrRelaService {

    @Override
    public void saveGameAttrRelaInfo(GameAttrReqVo gameAttrReqVo) {
        Long gameId = gameAttrReqVo.getGameId();
        if (GmMallUtils.isEmpty(gameId)) {
            throw new GameException("入参[gameId]不得为空", 10000);
        }
        Long attrGroupId = gameAttrReqVo.getAttrGroupId();
        if (GmMallUtils.isEmpty(attrGroupId)) {
            throw new GameException("入参[attrGroupId]不得为空", 10000);
        }
        List<String> attrIdValues = gameAttrReqVo.getAttrIdValues();
        log.info("attrIdValues : {}", attrIdValues);
        if (GmMallUtils.isEmpty(attrIdValues)) {
            throw new GameException("配置参数不得为空", 10000);
        }
        List<GameAttrRelaEntity> gameAttrRelaEntities = new ArrayList<>();
        attrIdValues.forEach(string -> {
            GameAttrRelaEntity gameAttrRelaEntity = new GameAttrRelaEntity();
            gameAttrRelaEntity.setGameId(gameId);
            gameAttrRelaEntity.setAttrGroupId(attrGroupId);
            String[] attrs = string.split("attr");
            gameAttrRelaEntity.setAttrId(Long.valueOf(attrs[0]));
            gameAttrRelaEntity.setValue(attrs[1]);
            gameAttrRelaEntities.add(gameAttrRelaEntity);
        });

        this.saveBatch(gameAttrRelaEntities);
    }
}
